﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LeafBookmark.Data
{
    interface IBookmark
    {
        string Title
        {
            get;
            set;
        }

        Uri Url
        {
            get;
            set;
        }

        DateTime CreatedTime
        {
            get;
        }
    }

    /// <summary>
    /// Bookmark of Microsoft Internet Explorer
    /// </summary>
    class BookmarkOfExplorer : IBookmark
    {
        #region IBookmark members
        
        public string Title {get; set;}

        public Uri Url {get; set;}
        
        public DateTime CreatedTime 
        { 
            get { return createdTime; }
        }
        
        #endregion

        // the created time of this bookmark
        private DateTime createdTime;

        // the shortcut filename of this bookmark.
        // Internet explorer bookmark saved as a shortcut file.
        private FileInfo fileInfo;

        public BookmarkOfExplorer(FileInfo file)
        {
            this.fileInfo = file;
            Load();
        }

        public BookmarkOfExplorer(string title, string url, DateTime time)
        {
            Title = title;
            Url = new Uri(url);
            createdTime = time;
        }

        private void Load()
        {
            Title = fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length);
            using(StreamReader reader = File.OpenText(fileInfo.FullName))
            {
                string line;
                while((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("URL=", StringComparison.OrdinalIgnoreCase)) 
                    {
                        Url = new Uri(line.Substring(4));
                    }
                }
            }
        }

    }

}
